<?php


namespace app\controller;


use think\facade\Db;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class WxPay
{
    protected $config = [];

    public function __construct()
    {
        $this->config['appid'] = env("WECHAT_H5APPID");
        $this->config['app_id'] = env("WECHAT_H5APPID");
        $this->config['miniapp_id'] = env("WECHAT_H5APPID");
        $this->config['mch_id'] = env("WECHAT_MCHID");
        $this->config['key'] = env("WECHAT_PAYKEY");
        $this->config['notify_url'] = env("WECHAT_PAYSUCCESSCALL");
        $this->config['mode'] = env("WECHAT_MODE");
    }

    // 创建称重 通过 订单
    public function createWeightPassOrder()
    {

        $req = Request();
        if($req->method() !== "POST"){
            return 'fail';
        }
        $openid = $req->openid;
        // 创建订单之前 先查询 一小时内 未支付的订单 如果有，则直接返回订单号

        $order = [
            'order_num' => randomStr(),
            'price' => 1000,// 支付金额  单位 分
            'status'=> 0,// 默认0 未支付   10 已支付
            'create_time' => date("Y-m-d H:i:s",time()),
            'wx_h5_openid' => $openid,
            'car_id' => $req->post("car_id")
        ];
        Db::table("Sys_WeightPassOrder")->save($order);

        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => [
                'order_num'=>$order['order_num']
            ]
        ]);
    }


    public function payWeightPassOrder()
    {
        $req = Request();
        $orderNum = $req->post("order_num");
        $order = Db::table("Sys_WeightPassOrder")->where("order_num",$orderNum)->find();
        $order = [
            'out_trade_no' => $order['order_num'],
            'body' => '称重费用',
            'total_fee' => $order['price'],
            'openid' => $req->openid,
        ];
        $result = Pay::wechat($this->config)->mp($order);
        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $result
        ]);
    }


    public function paySuccess(){



        $data = Pay::wechat($this->config)->verify()->toArray();

        $file = fopen("pay-log.json", "w");
        fwrite($file, json_encode($data));
        fclose($file);
        
        $orderSn = $data['out_trade_no'] ?? '';
        



        Db::table("Sys_WeightPassOrder")->where("order_num",$orderSn)->update([
            'pay_time'=>date("Y-m-d H:i:s",time()),
            'status'=>10
        ]);
        return Pay::wechat($this->config)->success();
    }


    /**
     * 获取支付记录（订单）
     */
    public function getOrderList(){
        $page = input("pageNum");
      
        $openid = Request()->openid;
        $data = Db::table("Sys_WeightPassOrder")
        ->where("wx_h5_openid",$openid)
        ->paginate([
            'page'=>$page
        ]);

        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $data
        ]);
    }
}